//
//  ConfirmOrderVC.m
//  jingdongfang
//
//  Created by MTiMac01 on 2017/8/24.
//  Copyright © 2017年 JiongLiang Li. All rights reserved.
//

#import "ConfirmOrderVC.h"
#import "AliPayManager.h"
#import "WeiXinPayManager.h"
#import "BuySuccViewController.h"

@interface ConfirmOrderVC ()<UITableViewDelegate, UITableViewDataSource>
{
    NSInteger _payType;//1支付宝 2微信
    NSString *orderId;// 订单id
    NSString * _payPrice;// 需要支付的价格
}
@property (nonatomic, strong)UIView *buyView;
@property (nonatomic, strong)UILabel *moneyL;
@end

@implementation ConfirmOrderVC

- (void)viewDidLoad
{
    self.title = @"提交订单";
    [super viewDidLoad];
    self.tableView.frame = CGRectMake(0, 200, KSCREENWIDTH, KSCREENHEIGHT -200);
    self.tableView.scrollEnabled = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self initBuyView];
    self.tableView.backgroundColor = RGB(234, 234, 234);
    _payType = 1;
    //注册监听-支付宝
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(dealAlipayResult:) name:@"alipayResult" object:nil];
    //注册监听-微信
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(dealWXpayResult:) name:@"WXpayresult" object:nil];
}

- (void)initBuyView
{    
    self.buyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, 50)];
    self.buyView.bottom = KSCREENHEIGHT - 64;
    self.buyView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.buyView];
    
    self.moneyL = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH / 3 * 2 - 20, 50)];
    self.moneyL.backgroundColor = [UIColor whiteColor];
//    self.moneyL.text = [NSString stringWithFormat:@"实付款: ¥%.2f", [self.detailsInfo.material_price floatValue] - [self.detailsInfo.artcoin floatValue]/10];
    self.moneyL.textColor = [UIColor c1Color];
    self.moneyL.font = [UIFont systemFontOfSize:20];
    self.moneyL.textAlignment = NSTextAlignmentRight;
    [self.buyView addSubview:self.moneyL];
    
    UIButton *buy = [UIButton buttonWithType:UIButtonTypeCustom];
    buy.frame = CGRectMake(KSCREENWIDTH / 3 * 2, 0, KSCREENWIDTH / 3, 50);
    buy.backgroundColor = [UIColor c1Color];
    [buy setTitle:@"去付款" forState:UIControlStateNormal];
    [buy setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [buy addTarget:self action:@selector(subAction:) forControlEvents:UIControlEventTouchUpInside];
    buy.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.buyView addSubview:buy];
}

//- (void)setDetailsInfo:(DetailsInfo *)detailsInfo
//{
//    if (_detailsInfo != detailsInfo)
//    {
//        _detailsInfo = detailsInfo;
//    }
//
//}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    CGFloat h = [self tableView:tableView heightForRowAtIndexPath:indexPath];
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 0, 35, 35)];
    imgView.centerY = h/2;
    imgView.contentMode = UIViewContentModeScaleAspectFit;
    
    UIImageView *acc = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    acc.contentMode = UIViewContentModeScaleAspectFit;
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectZero];
    title.font = [UIFont systemFontOfSize:15];
    if (indexPath.row==0)
    {
        cell.textLabel.text = @"选择支付方式";
    }
    else if (indexPath.row==1)
    {
        imgView.image = [UIImage imageNamed:@"APP buy 切图 20160727-4"];
        [cell.contentView addSubview:imgView];
        
        title.text = @"支付宝";
        [title sizeToFit];
        [cell.contentView addSubview:title];
        title.centerY = h/2;
        title.left = imgView.right + 18;
        if (_payType == 1)
        {
            acc.image = [UIImage imageNamed:@"未标题-1-2"];
        }
        else
        {
            acc.image = [UIImage imageNamed:@"未标题-1-1"];
        }
        cell.accessoryView = acc;
    }
    else if (indexPath.row==2)
    {
        imgView.image = [UIImage imageNamed:@"APP buy 切图 20160727-5"];
        [cell.contentView addSubview:imgView];
        
        title.text = @"微信";
        [title sizeToFit];
        [cell.contentView addSubview:title];
        title.centerY = h/2;
        title.left = imgView.right + 18;
        
        if (_payType == 2)
        {
            acc.image = [UIImage imageNamed:@"未标题-1-2"];
        }
        else
        {
            acc.image = [UIImage imageNamed:@"未标题-1-1"];
        }
        cell.accessoryView = acc;
        
        UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(25, 0, KSCREENWIDTH-25, .5)];
        line.backgroundColor = RGB(234, 234, 234);
        [cell.contentView addSubview:line];
    }
    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    _payType = indexPath.row;
    [tableView reloadData];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0)
    {
        return 45;
    }
    return 65;
}
#pragma mark - 提交订单
- (void)subAction:(UIButton *)btn
{
    if(_payType == 1)
    {  //1.  支付宝
        [self aliPayWith];
    }
    if(_payType == 2)
    {  // 2 微信
        [self wechatPayWith];
    }

}

- (void)aliPayWith
{
    Product *p = [[Product alloc] init];
    p.price = [self.moneyL.text substringFromIndex:self.moneyL.text.length- 4];
//    p.subject =self.detailsInfo.title;
    p.orderId = self.orders;
    // 支付宝支付
    [[AliPayManager sharedAliPayManager] pay:p];
}

- (void)wechatPayWith{
    
    
    //get:/app.php/Wx/index
    //    orderids#订单id
    //    order_price#订单金额
    //    product_name#订单名称
    //
    
    
//    NSDictionary * dictionary = @{@"orderids":self.orders,
//                                  @"order_price":[self.moneyL.text substringFromIndex:self.moneyL.text.length - 4],
//                                  @"product_name":self.detailsInfo.title};
//    NSString *str = @"app.php/Wx/index";
//    [MCHttp postPayRequestURLStr:[NSString stringWithFormat:@"%@%@",kBaseUrl, str] withDic:dictionary success:^(NSDictionary *requestDic, NSString *msg) {
//        
//        NSLog(@"---  %@",requestDic);
//        [self dismissLoading];
//        
//        
//        WXProduct * wxProduct = [WXProduct yy_modelWithJSON:requestDic];
//        [[WeiXinPayManager sharedManager]weiXinPay:wxProduct];
//
//        
//    } failure:^(NSString *error) {
//        [self dismissLoading];
//        
//    }];
    
}
#pragma mark - 支付宝支付状态回调
-(void)dealAlipayResult:(NSNotification*)notification{
    NSString*result=notification.object;
    if([result isEqualToString:@"9000"]){
        //在这里写支付成功之后的回调操作
        NSLog(@"支付宝支付成功");
        
        BuySuccViewController *vc = [[BuySuccViewController alloc] init];
        vc.orderId = self.o_id;
        BaseNavigationController *nav = [[BaseNavigationController alloc] initWithRootViewController:vc];
        [Tool performBlock:^{
            [self.navigationController presentViewController:nav animated:YES completion:^{
                [self.navigationController popToRootViewControllerAnimated:YES];
            }];
        } afterDelay:1];
        
    }else{
        //在这里写支付失败之后的回调操作
        //DeLog(@"支付宝支付失败");
        [self.navigationController popViewControllerAnimated:YES];
        
    }
    
}

#pragma mark - 微信支付状态的回调

-(void)dealWXpayResult:(NSNotification*)notification{
    NSString*result=notification.object;
    if([result isEqualToString:@"1"]){
        //在这里写支付成功之后的回调操作
        NSLog(@"微信支付成功");
        
        BuySuccViewController *vc = [[BuySuccViewController alloc] init];
        vc.orderId = self.o_id;
        BaseNavigationController *nav = [[BaseNavigationController alloc] initWithRootViewController:vc];
        [Tool performBlock:^{
            [self.navigationController presentViewController:nav animated:YES completion:^{
                [self.navigationController popToRootViewControllerAnimated:YES];
            }];
        } afterDelay:1];
    }else{
        //在这里写支付失败之后的回调操作
        // DeLog(@"微信支付失败");
        [self.navigationController popViewControllerAnimated:YES];
    }
}

//---------------------------------------------

@end
